package com.souku.finance.application.base;

import android.content.pm.ActivityInfo;
import android.graphics.Bitmap;
import android.net.http.SslError;
import android.os.Bundle;
import android.view.View;
import android.webkit.SslErrorHandler;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.gc.materialdesign.views.ProgressBarDeterminate;
import com.souku.finance.application.R;
import com.souku.finance.application.utils.ChooseWebUtil;

public class BaseWebviewActivity extends BaseActivity implements View.OnClickListener {

    protected RelativeLayout topBack;
    protected TextView topTitle;
    protected WebView web_view;
    protected ProgressBarDeterminate progressDeterminate;
    private String url;
    private String content;
    private String title;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setContentView(R.layout.web_view);
        initView();
        url = getIntent().getStringExtra("url");
        content = getIntent().getStringExtra("content");
        title = getIntent().getStringExtra("topTitle");
        web_view.setBackgroundColor(2);
        progressDeterminate.setProgress(100);
        web_view.getSettings().setJavaScriptEnabled(true);
        web_view.getSettings().setBlockNetworkImage(false);
        web_view.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
        web_view.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                super.onProgressChanged(view, newProgress);
                progressDeterminate.setProgress(newProgress);
                if (newProgress == 100) {
                    progressDeterminate.setVisibility(View.GONE);
                }
            }
        });
        web_view.setWebViewClient(new WebViewClient() {

            @Override
            public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
                handler.proceed();
            }

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                // 返回值是true的时候控制去WebView打开，为false调用系统浏览器或第三方浏览器
                if (ChooseWebUtil.isContain(url)) {
                    ChooseWebUtil.startActivity(BaseWebviewActivity.this, url);
                } else {
                    view.loadUrl(url);
                }
                return true;
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                if (content == null) {
                    String s = view.getTitle();
                    if (s != null) {
                        if (!s.contains("com")) {
                            topTitle.setText(s);
                        }
                    } else {
                        topTitle.setText(title);
                    }
                }
            }

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
            }
        });
        if (content == null) {
            web_view.loadUrl(url);
        } else {
            web_view.getSettings().setDefaultTextEncodingName("UTF-8");
            web_view.loadDataWithBaseURL(null, content, "text/html", "UTF-8", null);
        }
    }

    @Override
    public void onBackPressed() {
        if (web_view.canGoBack()) {
            //判断是否是第一级H5
            if (web_view.getUrl().equals(url)) {
                super.onBackPressed();
            } else {
                //webview的返回上一级
                web_view.goBack();
            }
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.top_back) {
            finish();
        }
    }

    private void initView() {
        topBack = (RelativeLayout) findViewById(R.id.top_back);
        topBack.setOnClickListener(BaseWebviewActivity.this);
        topTitle = (TextView) findViewById(R.id.top_title);
        web_view = (WebView) findViewById(R.id.web_view);
        progressDeterminate = (ProgressBarDeterminate) findViewById(R.id.progressDeterminate);
    }

    @Override
    protected void onResume() {
        if (getRequestedOrientation() != ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        }
        super.onResume();
    }

    @Override
    public void internet() {

    }
}
